package com.dhnsoft.education.controller.order;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhnsoft.education.entity.order.Order;
import com.dhnsoft.education.service.order.OrderService;
import com.dhnsoft.education.utils.JwtUtils;
import com.dhnsoft.education.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 代浩楠
 * @since 2022-04-14
 */
@RestController
@RequestMapping("/eduorder/order")
//@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;



    //1.生成订单方法
    @GetMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            return R.error().code(28004).message("请登录");
        }
        //创建订单，返回订单号
        String orderNo = orderService.createOrders(courseId,memberId);
        return R.ok().data("orderId",orderNo);
    }
    //根据用户id获取所拥有的订单
    @GetMapping("getOrderByUserId")
    public R getOrderByUserId(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            return R.error().code(28004).message("请登录");
        }
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        List<Order> list = orderService.list(wrapper);
        return R.ok().data("orderList",list);
    }

    //2.根据订单id查询订单信息
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item",order);
    }

    //根据课程id和用户id查询订单表中订单状态
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public Boolean isBuyCourse(@PathVariable String courseId, @PathVariable String memberId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        int count = orderService.count(wrapper);
        System.out.println("222323:"+memberId);
        if(count>0){
            return true;
        }
        return false;
    }

    //条件带分页查询订单信息
    @PostMapping("pageOrderInfo/{current}/{limit}")
    public R pageOrderInfo(@PathVariable long current, @PathVariable long limit, @RequestBody Order orderQuery){
        Page<Order> orderPage = new Page<>(current,limit);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        String teacherName = orderQuery.getTeacherName();//订单中的讲师名称
        Integer status = orderQuery.getStatus();//订单状态
        String nickname = orderQuery.getNickname();//会员昵称
        Date gmtCreate = orderQuery.getGmtCreate();//订单创建时间
        Integer payType = orderQuery.getPayType();//支付类型
        String orderNo = orderQuery.getOrderNo();//订单号
        if(!StringUtils.isEmpty(teacherName)){
            wrapper.like("teacher_name",teacherName);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(nickname)){
            wrapper.like("nickname",nickname);
        }
        if(!StringUtils.isEmpty(gmtCreate)){
            wrapper.eq("gmt_create",gmtCreate);
        }
        if(!StringUtils.isEmpty(orderNo)){
            wrapper.eq("order_no",orderNo);
        }
        if(!StringUtils.isEmpty(payType)){
            wrapper.eq("pay_type",payType);
        }
        orderService.page(orderPage,wrapper);
        //调用方法实现分页条件查询
        long total = orderPage.getTotal();//总记录数
        List<Order> records = orderPage.getRecords();//数据list集合数
        return R.ok().data("total",total).data("rows",records);
    }
}

